package com.samuel.aware;


import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @date:2017/11/23
 * @time: 14:26
 * @author:zhaoyang
 * @desc:Spring Aware Demo
 *
 *   BeanNameAware:获得Bean 名称
 *  ResourceLoaderAware ： 获得资源加载器
 *
 *
 **/



@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware {

    private String beanName;
    private ResourceLoader resourceLoader;

    @Override
    public void setBeanName(String s) {
        this.beanName=s;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
            this.resourceLoader=resourceLoader;
    }

    public void outputResource(){

        System.out.println("BeanName 是 ["+ beanName +"]");

        Resource resource = resourceLoader.getResource("classpath:test.txt");

        try {
            /*System.out.println("resource 加载的内容是：【"+resource.getInputStream()+"】");*/
            System.out.println(IOUtils.toString(resource.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
